package wms.handler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import wms.entity.DataDict;
import wms.entity.Goods;
import wms.entity.Operator;
import wms.service.goods.GoodsService;
import wms.util.PageUtil;
import wms.util.constant.SysConstant;

@Controller
public class GoodsController extends BaseController{
	
	@Resource
	private GoodsService GoodsService;
	
	/**
	 * 物资管理页面
	 * @param model
	 * @param session
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/base/goodslist.html")
	public String getGoodListPage(Model model, HttpSession session,
				@RequestParam(value="goodsName", required = false) String goodsName, @RequestParam(value="goodsCode", required = false) String goodsCode,
				@RequestParam(value="type", required = false) String type,
				@RequestParam(value="pageIndex",required=false) String curIndex, @RequestParam(value="pageSize",required=false) String pageSize) throws Exception{
		log.debug("物资信息页面------->");
		
		List<DataDict> goodsTypeList = getDataDictService().listDataDict(SysConstant.DICT_GOODS_TYPE);
		List<Goods> goodsList = null;
		
		//分页查询
		Integer currentIndex = null;
		Integer pageSizes = null;
		if (Objects.isNull(curIndex) || ("").equals(curIndex)) {
			currentIndex = SysConstant.DEFAULT_CURRNET_INDEX;
		} else {
			currentIndex = Integer.valueOf(curIndex);
		}
		
		if(Objects.isNull(pageSize) || ("").equals(pageSize)){
			pageSizes = SysConstant.DEFAULT_PAGESIZE;
		} else {
			pageSizes = Integer.valueOf(pageSize);
		}
		
		//类型编号
		Integer typeId = null;
		if (type == null || type.equals(""))
			typeId = null;
		else {
			typeId = Integer.valueOf(type);
			
		}
		
		//Page<Goods> pages = PageHelper.startPage(currentIndex, pageSizes);
		PageUtil pages = new PageUtil(currentIndex, pageSizes);
		goodsList = GoodsService.listAllGoodsWithParam(goodsName, goodsCode, typeId, (pages.getPage() - 1) * pages.getPageSize(), pages.getPageSize());
		
		if (goodsList != null && goodsList.size() > 0) {
			//总记录数量
			pages.setTotalCount(
					GoodsService.getTotalCount(goodsName, goodsCode, typeId));
			pages.setPage(currentIndex);
			if (pages.getPage() < 1)
				pages.setPage(1);
			if (pages.getPage() > pages.getTotalCount()) 
				pages.setPage(pages.getTotalCount());
			
			pages.setList(goodsList);
		} else 
			pages.setList(null);
		
		
		model.addAttribute("pages", pages);
		model.addAttribute(SysConstant.GOODS_LIST, goodsList);
		model.addAttribute(SysConstant.DICT_GOODS_TYPE_LIST, goodsTypeList);  
		
		//回显
		model.addAttribute("goodsCode", goodsCode);
		model.addAttribute("goodsName", goodsName);
		model.addAttribute("type", typeId);
		
		return "goods/goodslist";
	}
	
	
	@RequestMapping(value = "/basic/goods/modify")
	@ResponseBody
	public Map<String, String> getModifGoodsPage(@RequestParam(value = "goodsCode") String goodsCode) throws Exception {
		log.debug("物资修改页面--------->" + goodsCode);
		Map<String, String> map = new HashMap<>();
		Goods goods = GoodsService.getGoods(goodsCode);
		if (goods != null) {
			map.put(SysConstant.MSG, JSONObject.toJSONString(goods));
		} else 
			map.put(SysConstant.MSG, SysConstant.FAIL);
		return map;
	}
	
	
	@RequestMapping(value = "/basic/goods/savemodify", method = RequestMethod.POST)
	public String modifyGoods(Goods goods, HttpSession session, HttpServletRequest request) throws Exception {
		log.debug("物资修改保存--------->");
		Operator operator = (Operator) session.getAttribute(SysConstant.OPERATOR);
		if (operator == null)
			return "redirect:/";
		
		if (GoodsService.modifyGoods(goods, operator.getLoginCode())) {
			return "redirect:/base/goodslist.html";
		} else{
			request.setAttribute(SysConstant.MSG, SysConstant.FAIL);
			return "redirect:/";
		}
		
	}
	
	
	/**
	 * 查看物资
	 * @param goodsCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/basic/goods/view")
	@ResponseBody
	public Map<String, Object> viewGoods(@RequestParam(value = "goodsCode") String goodsCode) throws Exception {
		log.debug("查看物资----------》" + goodsCode);
		Map<String, Object> map = new HashMap<String, Object>();
		if (goodsCode == null || goodsCode.equals("")) {
			map.put(SysConstant.MSG, SysConstant.FAIL);
		} else{
			Goods goods = GoodsService.getGoods(goodsCode);
			if (goods != null) {
				map.put(SysConstant.MSG, goods);
			} else 
				map.put(SysConstant.MSG, SysConstant.FAIL);
		}
		
		return map;
	}
	
	
}
